package gui.admin;

import gui.AbstractDefensiveDriverPanel;

import java.sql.SQLException;

import objects.Instructor;
import objects.School;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import database.CSdb;



public class AbstractAdminPanelImpl extends AbstractDefensiveDriverPanel {

	protected Menu menuBar, fileMenu, helpMenu;
	protected MenuItem fileHeader, helpHeader, fileNewItem, fileExitItem, helpAboutItem,
		fileDriversItem;
	protected CSdb database;
	protected Instructor instructor = new Instructor(1);
	protected School school = new School(1);
	
	public AbstractAdminPanelImpl(Display display, Shell shell) {
		super(display, shell);
		
		menuBar = new Menu(shell, SWT.BAR);
		fileHeader = new MenuItem(menuBar, SWT.CASCADE);
		fileHeader.setText("&File");
		
		fileMenu = new Menu(shell, SWT.DROP_DOWN);
		fileHeader.setMenu(fileMenu);
		
		fileNewItem = new MenuItem(fileMenu, SWT.PUSH);
	    fileNewItem.setText("&New");
		
	    fileDriversItem = new MenuItem(fileMenu, SWT.PUSH);
	    fileDriversItem.setText("Drivers");
	    
		fileExitItem = new MenuItem(fileMenu, SWT.PUSH);
	    fileExitItem.setText("E&xit");
	    
	    helpHeader = new MenuItem(menuBar, SWT.CASCADE);
	    helpHeader.setText("&Help");

	    helpMenu = new Menu(shell, SWT.DROP_DOWN);
	    helpHeader.setMenu(helpMenu);

	    helpAboutItem = new MenuItem(helpMenu, SWT.PUSH);
	    helpAboutItem.setText("&About");
	    
	    fileExitItem.addSelectionListener(new FileExitItemListener());
	    
	    FileNewItemListener fileNewItemListener = new FileNewItemListener();
	    fileNewItemListener.setParent(this);
	    fileNewItem.addSelectionListener(fileNewItemListener);
	    
	    helpAboutItem.addSelectionListener(new HelpAboutItemListener());
	    fileDriversItem.addSelectionListener(new FileDriversItemListener());
	    
	    shell.setMenuBar(menuBar);
	    
	    try {
			database = new CSdb();
		} catch (Exception e) {
			System.out.println("Unable to connect to Server");
			e.printStackTrace();
		}
		
		try {
			database.getInstructor(instructor);
		} catch (SQLException e) {
			System.out.println("Unable to get instructor");
			e.printStackTrace();
		}
		
		try {
			database.getSchool(school);
		} catch (SQLException e) {
			System.out.println("Unable to get school");
			e.printStackTrace();
		}
	}
	
	protected class FileExitItemListener implements SelectionListener {
		public void widgetSelected(SelectionEvent event) {
			database.close();
			shell.close();
			display.dispose();
		}

	    public void widgetDefaultSelected(SelectionEvent event) {
	    	database.close();
	    	shell.close();
	    	display.dispose();
	    }
	}
	
	private class FileNewItemListener implements SelectionListener {

		private AbstractAdminPanelImpl parent;
		
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			setEnabled(false);
			AdminNewDriverPanel window = new AdminNewDriverPanelImpl(display, new Shell());
			window.setCaller(parent);
			window.open();
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			setEnabled(false);
			AdminNewDriverPanel window = new AdminNewDriverPanelImpl(display, new Shell());
			window.setCaller(parent);
			window.open();
		}
		
		public void setParent(AbstractAdminPanelImpl parent) {
			this.parent = parent;
		}
	}
	
	private class HelpAboutItemListener implements SelectionListener {
		
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			dispose();
			AdminAboutPanel window = new AdminAboutPanelImpl(display, shell);
			window.open();
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			dispose();
			AdminAboutPanel window = new AdminAboutPanelImpl(display, shell);
			window.open();
		}	
	}
	
	private class FileDriversItemListener implements SelectionListener {

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			dispose();
			AdminDriverDisplayPanel window = new AdminDriverDisplayPanelImpl(display, shell);
			window.open();
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			dispose();
			AdminDriverDisplayPanel window = new AdminDriverDisplayPanelImpl(display, shell);
			window.open();
		}
	}
	
	public void closeDatabase() {
		database.close();
	}
}
